<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Album\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Album\Form\AlbumForm;
use Album\Form\AlbumFilter;
use Album\Entity\Album;
use Doctrine\ORM\EntityManager;
use Zend\View\Model\JsonModel;

class AlbumController extends AbstractActionController
{
    protected $albumTable;
    protected $em;
    
    public function indexAction()
    {
        $results = $this->getAlbumTable()->fetchAll();
        $data = array();
        foreach($results as $result) {
            $data[] = $result;
        }
        //print_r($data);
        //exit;
        return new JsonModel(array('data' => $data));
        //return new ViewModel(array('albums' => $this->getAlbumTable()->fetchAll()));
        //return new ViewModel(array('albums' => $this->getEntityManager()->getRepository('Album\Entity\Album')->findAll()));
        //return new ViewModel();
    }
    
    public function addAction()
    { 
        $form = new AlbumForm();
        $form->get('submit')->setAttribute('label' , 'Add');
        $request = $this->getRequest();
        if($request->isPost()){ 
           $album = new Album();
            $formFilter = new AlbumFilter();
            $form->setInputFilter($formFilter);
            $form->setData($request->getPost());
            if ($form->isValid()) {                
                $album->populate($form->getData()); 
                $this->getEntityManager()->persist($album);
                $this->getEntityManager()->flush();
 
                // Redirect to list of albums
                return $this->redirect()->toRoute('album'); 
 
            }
            else{
                //die(print 'not ok');
            }
        }
        return array('form' => $form);
    }
    
    public function editAction()
    {
        $id = (int)$this->getEvent()->getRouteMatch()->getParam('id');
        if (!$id) {
            return $this->redirect()->toRoute('album', array('action'=>'add'));
        } 
        $album = $this->getEntityManager()->find('Album\Entity\Album', $id);
 
        $form = new AlbumForm();
        $form->setBindOnValidate(false);
        $form->bind($album);
        $form->get('submit')->setAttribute('label', 'Edit');
        
        $request = $this->getRequest();
        if ($request->isPost()) {
            $form->setData($request->post());
            if ($form->isValid()) {
                $form->bindValues();
                $this->getEntityManager()->flush();
 
                // Redirect to list of albums
                return $this->redirect()->toRoute('album');
            }
        }
 
        return array(
            'id' => $id,
            'form' => $form,
        );
    }
    
    public function deleteAction()
    {
        
    }
    
    public function getAlbumTable()
    {
        if(!$this->albumTable)
        {
            $sm = $this->getServiceLocator();
            $this->albumTable = $sm->get('Album\Model\AlbumTable');            
        }
        return $this->albumTable;
    }
    
    public function setEntityManager(EntityManager $em)
    {
        $this->em = $em;
    }
    
    public function getEntityManager()
    {
        if(null === $this->em)
        {
            $this->em = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default');
        }
        return $this->em;
    }
}